package com.wcpdoc.exam.core.dao.impl;

import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.wcpdoc.base.dao.UserDao;
import com.wcpdoc.core.dao.impl.RBaseDaoImpl;
import com.wcpdoc.core.entity.PageIn;
import com.wcpdoc.core.entity.PageOut;
import com.wcpdoc.core.util.DateUtil;
import com.wcpdoc.core.util.HibernateUtil;
import com.wcpdoc.core.util.SqlUtil;
import com.wcpdoc.core.util.SqlUtil.Order;
import com.wcpdoc.core.util.ValidateUtil;
import com.wcpdoc.exam.core.dao.ExerDao;
import com.wcpdoc.exam.core.entity.Exer;

/**
 * 练习数据访问层实现
 * 
 * v1.0 chenyun 2021-03-02 13:43:21
 */
@Repository
public class ExerDaoImpl extends RBaseDaoImpl<Exer> implements ExerDao {
    @Resource
    private UserDao userDao;
	
	@Override
	public PageOut getListpage(PageIn pageIn) {
		String sql = "SELECT EXER.ID AS ID, EXER.NAME, EXER.START_TIME, EXER.END_TIME, "
				+ "EXER.RMK_STATE, EXER.USER_IDS, "
				+ "QUESTION_TYPE.NAME AS QUESTION_TYPE_NAME, "
				+ "(SELECT COUNT(1) FROM EXM_QUESTION WHERE STATE=1 AND QUESTION_TYPE_ID = EXER.QUESTION_TYPE_ID) AS QUESTION_NUM "
				+ "FROM EXM_EXER EXER "
				+ "LEFT JOIN EXM_QUESTION_TYPE QUESTION_TYPE ON EXER.QUESTION_TYPE_ID = QUESTION_TYPE.ID ";
		SqlUtil sqlUtil = new SqlUtil(sql);
		sqlUtil.addWhere(ValidateUtil.isValid(pageIn.get("questionTypeId", Integer.class)) , 
						"EXER.QUESTION_TYPE_ID = :QUESTION_TYPE_ID", pageIn.get("questionTypeId", Integer.class))
				.addWhere(ValidateUtil.isValid(pageIn.get("name")), "EXER.NAME LIKE :NAME", String.format("%%%s%%", pageIn.get("name")))
				.addWhere("true".equals(pageIn.get("todo")), ":CUR_TIME <= EXER.END_TIME", new Date())// 查找我的未完成的练习列表
				.addWhere(ValidateUtil.isValid(pageIn.get("startTime")) && ValidateUtil.isValid(pageIn.get("endTime")), 
						"(( :START_TIME1 <= EXER.START_TIME AND EXER.START_TIME <= :END_TIME1) "
						+ "	OR ( :_START_TIME2 <= EXER.END_TIME AND EXER.END_TIME <= :END_TIME2) "
						+ "	OR ( :START_TIME3 >= EXER.START_TIME AND EXER.END_TIME >= :END_TIME3))", 
						pageIn.get("startTime"), pageIn.get("endTime"),
						pageIn.get("startTime"), pageIn.get("endTime"),
						pageIn.get("startTime"), pageIn.get("endTime")
						)
				.addWhere(ValidateUtil.isValid(pageIn.get("curUserId", Integer.class)), // 子管理员登录，各看各的
						"EXER.CREATE_USER_ID = :CREATE_USER_ID", pageIn.get("curUserId", Integer.class))
				.addWhere(ValidateUtil.isValid(pageIn.get("examUserId", Integer.class)),// 考试用户，看自己的
						"(EXER.USER_IDS LIKE :USER_IDS)", String.format("%%,%s,%%", pageIn.get("examUserId", Integer.class)))
				.addWhere("EXER.STATE = 1")
				.addOrder("EXER.START_TIME", Order.DESC);
		PageOut pageOut = getListpage(sqlUtil, pageIn);
		HibernateUtil.formatDate(pageOut.getList(), "startTime", DateUtil.FORMAT_DATE_TIME);
		HibernateUtil.formatDate(pageOut.getList(), "endTime", DateUtil.FORMAT_DATE_TIME);
		return pageOut;
	}

	@Override
	public List<Exer> getList(Integer questionTypeId) {
		String sql = "SELECT * FROM EXM_EXER WHERE QUESTION_TYPE_ID = :QUESTION_TYPE_ID AND STATE = 1 ";
		return getList(sql, new Object[] { questionTypeId });
	}

	/**
	 * 某人可以练习的题库
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public PageOut listExer(Integer userId) {
		String sql="select \n" +
				"a.ID,\n" +
				"a.NAME, \n" +
				"(SELECT COUNT(1) FROM EXM_QUESTION WHERE STATE=1 AND QUESTION_TYPE_ID = a.ID) AS QUESTION_NUM \n" +
				"from exm_question_type a \n" +
				"inner join exm_user_question_type b on a.ID=b.QUESTION_TYPE_ID\n" +
				"WHERE b.STATE=1 and b.USER_ID=:userId \n" +
				"ORDER BY a.SORT ASC";
		final List<Map<String,Object>> list = getMapList(sql, new Object[]{userId});
		return new PageOut(list, list.size());
	}

	/**
	 * 根据用户权限获取用户基础练习题型数量
	 *
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getMyQuestionTypeNum(Integer userId) {
		String sql = "select a.TYPE," +
				"COUNT(*) AS QUESTION_NUM " +
				"from exm_question a " +
				"inner join exm_user_question_type b on a.QUESTION_TYPE_ID=b.QUESTION_TYPE_ID " +
				"WHERE a.STATE=1 and b.STATE=1 and b.USER_ID=:userId GROUP BY a.TYPE ORDER BY a.TYPE ASC";

		List<Map<String, Object>> mapList = getMapList(sql, new Object[] { userId });
		return mapList;
	}

	/**
	 * 根据用户权限及题型获取用户基础练习题
	 *
	 * @return
	 */
	@Override
	public List<Integer> getBaseListByType(Integer userId,Integer type) {
		String sql = "select a.ID " +
				"from exm_question a " +
				"INNER join exm_user_question_type b on a.QUESTION_TYPE_ID=b.QUESTION_TYPE_ID " +
				"WHERE  a.STATE=1 and b.USER_ID=:userId and a.type=:type ORDER BY a.ID";
		return getMapList(sql, new Object[]{userId, type}).stream().map(m -> (int)m.get("id")).collect(Collectors.toList());
	}


	/**
	 * 根据用户权限及题型获取题库练习题
	 *
	 * @return
	 */
	@Override
	public List<Map<String,Object>> getPaperByQuestionType(Integer questionTypeId,Integer userId) {
		String sql ="select a.*,b.ANSWER," +
				"(case " +
				"WHEN c.ID is null then 0 " +
				"ELSE 1 " +
				"END) as isCollect " +
				"from exm_question a " +
				"inner join exm_question_answer b on a.id=b.QUESTION_ID " +
				"left join exm_my_question_collect c on a.ID=c.QUESTION_ID and c.TYPE=2 and c.USER_ID="+userId+" " +
				"where a.STATE=1 AND  a.QUESTION_TYPE_ID="+questionTypeId+" ";
		return getMapList(sql);
	}

	/**
	 * 根据用户权限及题型获取用户基础练习题
	 *
	 * @return
	 */
	@Override
	public List<Map<String,Object>> getPaperByType(Integer type,Integer userId) {
		String sql ="select a.*,b.ANSWER," +
				"(case " +
				"WHEN c.ID is null then 0 " +
				"ELSE 1 " +
				"END) as isCollect " +
				"from exm_question a " +
				"inner join exm_question_answer b on a.id=b.QUESTION_ID " +
				"left join exm_my_question_collect c on a.ID=c.QUESTION_ID and c.TYPE=2 and c.USER_ID="+userId+" " +
				"where a.STATE=1 AND a.TYPE="+type+"";
		return getMapList(sql);
	}
}